//package com.plian.system.service.tb.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.plian.Tools.CollectionUtil;
//import com.plian.Tools.DateUtil;
//import com.plian.system.dto.tb.InstitutionalStatisticsDTO;
//import com.plian.system.entity.sys.Dict;
//import com.plian.system.entity.tb.TripleMeeting;
//import com.plian.system.entity.tb.TripleMeetingTopic;
//import com.plian.system.properties.GzwProperties;
//import com.plian.system.service.sys.dict.IDictService;
//import com.plian.system.service.tb.ITripleMeetingService;
//import com.plian.system.service.tb.ITripleMeetingTopicService;
//import com.plian.system.service.tb.InstitutionalStatisticsService;
//import com.plian.system.vo.report.PieChartVO;
//import com.plian.system.vo.report.PieValueVO;
//import com.plian.system.vo.tb.MeetSumValue;
//import com.plian.system.vo.tb.TripleMeetingTopicVO;
//import com.plian.system.wrapper.tb.TripleMeetingTopicWrapper;
//import lombok.AllArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * @Author: Take-off
// * @Description: TODO 制度统计
// * @Date: 2023/1/9 9:50 AM
// * @Version: 1.0
// **/
//
//@Service
//@AllArgsConstructor
//public class InstitutionalStatisticsServiceImpl implements InstitutionalStatisticsService {
//
//    private final GzwProperties gzwProperties;
//
//    private final ITripleMeetingService meetingService;
//
//    private final ITripleMeetingTopicService topicService;
//
//    private final TripleMeetingTopicWrapper topicWrapper;
//
//    private final IDictService dictService;
//
//    @Override
//    public MeetSumValue getSumValue(InstitutionalStatisticsDTO dto){
//        MeetSumValue meetSumValue = new MeetSumValue();
//        List<TripleMeeting> tripleMeetings;
//        List<TripleMeeting> yearMeetings ;
//        Date now = DateUtil.now();
//        Date thisYear = DateUtil.parse(dto.getYear(), DateUtil.PATTERN_YEAR);
//        Date thisYearFinal = DateUtil.minusMillis(DateUtil.plusYears(thisYear,1),1);
//        if (dto.getOrgId().equals(gzwProperties.getId()) || dto.getOrgId().equals(gzwProperties.getReportId())){
//            //国资委
//            tripleMeetings = meetingService.list(new LambdaQueryWrapper<TripleMeeting>());
//            yearMeetings= meetingService.list(new LambdaQueryWrapper<TripleMeeting>().le(TripleMeeting::getMeetingTime, thisYearFinal).ge(TripleMeeting::getMeetingTime, thisYear));
//        }else {
//            //集团
//            tripleMeetings = meetingService.list(new LambdaQueryWrapper<TripleMeeting>().eq(TripleMeeting::getFillingUnitId,dto.getOrgId()));
//            yearMeetings= meetingService.list(new LambdaQueryWrapper<TripleMeeting>().eq(TripleMeeting::getFillingUnitId,dto.getOrgId())
//                    .le(TripleMeeting::getMeetingTime, thisYearFinal).ge(TripleMeeting::getMeetingTime, thisYear));
//        }
//        if (CollectionUtil.isNotEmpty(tripleMeetings)){
//            meetSumValue.setMeetingTotal(tripleMeetings.size());
//            List<TripleMeetingTopic> topics = topicService.list(new LambdaQueryWrapper<TripleMeetingTopic>()
//                    .in(TripleMeetingTopic::getBaseMeetingId, tripleMeetings.stream().map(TripleMeeting::getId).collect(Collectors.toList())));
//            if (CollectionUtil.isNotEmpty(topics)){
//                meetSumValue.setTopicTotal(topics.size());
//            }
//        }
//        if (CollectionUtil.isNotEmpty(yearMeetings)){
//            meetSumValue.setYearMeetingTotal(yearMeetings.size());
//            List<TripleMeetingTopic> topics = topicService.list(new LambdaQueryWrapper<TripleMeetingTopic>()
//                    .in(TripleMeetingTopic::getBaseMeetingId, yearMeetings.stream().map(TripleMeeting::getId).collect(Collectors.toList())));
//            if (CollectionUtil.isNotEmpty(topics)){
//                meetSumValue.setYearTopicTotal(topics.size());
//            }
//        }
//        return meetSumValue;
//    }
//
//    @Override
//    public PieChartVO getTopicTypeCake(InstitutionalStatisticsDTO dto){
//        PieChartVO pieChartVO = new PieChartVO();
//        List<PieValueVO> pieValueVOS = new ArrayList<>();
//        pieChartVO.setData(pieValueVOS);
//
//        Date thisYear = DateUtil.parse(dto.getYear(), DateUtil.PATTERN_YEAR);
//        Date thisYearFinal = DateUtil.minusMillis(DateUtil.plusYears(thisYear,1),1);
//        List<TripleMeeting> yearMeetings;
//        //指标
//        List<Dict> codes = dictService.list(new LambdaQueryWrapper<Dict>().eq(Dict::getParentId, "145"));
//        if (dto.getOrgId().equals(gzwProperties.getId()) || dto.getOrgId().equals(gzwProperties.getReportId())) {
//            //国资委
//            yearMeetings = meetingService.list(new LambdaQueryWrapper<TripleMeeting>().le(TripleMeeting::getMeetingTime, thisYearFinal).ge(TripleMeeting::getMeetingTime, thisYear));
//
//        }else {
//            //集团
//            yearMeetings= meetingService.list(new LambdaQueryWrapper<TripleMeeting>().eq(TripleMeeting::getFillingUnitId,dto.getOrgId())
//                    .le(TripleMeeting::getMeetingTime, thisYearFinal).ge(TripleMeeting::getMeetingTime, thisYear));
//        }
//        if (CollectionUtil.isNotEmpty(yearMeetings)){
//            List<TripleMeetingTopic> topics = topicService.list(new LambdaQueryWrapper<TripleMeetingTopic>()
//                    .in(TripleMeetingTopic::getBaseMeetingId, yearMeetings.stream().map(TripleMeeting::getId).collect(Collectors.toList())));
//            if (CollectionUtil.isNotEmpty(topics)){
//                for (Dict code:codes){
//                    PieValueVO valueVO = new PieValueVO();
//                    valueVO.setName(code.getName());
//                    List<TripleMeetingTopic> topicList = topics.stream().filter(m -> Optional.ofNullable(m.getTopicType()).isPresent() && m.getTopicType().equals(code.getCode())).collect(Collectors.toList());
//                    if (CollectionUtil.isNotEmpty(topicList)){
//                        valueVO.setValue(topicList.size());
//                    }else {
//                        valueVO.setValue(0);
//                    }
//                    pieValueVOS.add(valueVO);
//                }
//            }else {
//                for (Dict code:codes){
//                    PieValueVO valueVO = new PieValueVO();
//                    valueVO.setName(code.getName());
//                    valueVO.setValue(0);
//                    pieValueVOS.add(valueVO);
//                }
//            }
//        }
//
//        return pieChartVO;
//    }
//
//    @Override
//    public List<TripleMeetingTopicVO> getTopicList(InstitutionalStatisticsDTO dto){
//        List<TripleMeetingTopicVO> tripleMeetingTopics = new ArrayList<>();
//        Date thisYear = DateUtil.parse(dto.getYear(), DateUtil.PATTERN_YEAR);
//        Date thisYearFinal = DateUtil.minusMillis(DateUtil.plusYears(thisYear,1),1);
//        List<TripleMeeting> yearMeetings;
//        if (dto.getOrgId().equals(gzwProperties.getId()) || dto.getOrgId().equals(gzwProperties.getReportId())) {
//            //国资委
//            yearMeetings = meetingService.list(new LambdaQueryWrapper<TripleMeeting>().le(TripleMeeting::getMeetingTime, thisYearFinal).ge(TripleMeeting::getMeetingTime, thisYear));
//
//        }else {
//            //集团
//            yearMeetings= meetingService.list(new LambdaQueryWrapper<TripleMeeting>().eq(TripleMeeting::getFillingUnitId,dto.getOrgId())
//                    .le(TripleMeeting::getMeetingTime, thisYearFinal).ge(TripleMeeting::getMeetingTime, thisYear));
//        }
//        if (CollectionUtil.isNotEmpty(yearMeetings)) {
//            List<TripleMeetingTopic> topics = topicService.list(new LambdaQueryWrapper<TripleMeetingTopic>()
//                    .in(TripleMeetingTopic::getBaseMeetingId, yearMeetings.stream().map(TripleMeeting::getId).collect(Collectors.toList())));
//            if (CollectionUtil.isNotEmpty(topics)){
//                tripleMeetingTopics = topicWrapper.entityToVO(
//                        topics.stream().sorted(Comparator.comparing(TripleMeetingTopic::getUpdateTime).reversed()).collect(Collectors.toList()));
//            }
//        }
//
//        return tripleMeetingTopics;
//    }
//
//}
